import java.util.Stack;

public class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minstack;
    public MinStack() {
        stack = new Stack<>();
        minstack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        //第一次在最小栈当中存储元素
        if (minstack.empty()){
            minstack.push(val);
        }else {
            if (val <= minstack.peek()){
                minstack.push(val);
            }
        }
    }

    public void pop() {
        //栈为空，不能弹出
        if (stack.empty()){
            return;
        }
        int val = stack.pop();
        if (val == minstack.peek()){
            minstack.pop();
        }
    }

    //获取栈顶元素，和最小栈没有关系
    public int top() {
        if (stack.empty()){
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        return minstack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(-2);
        minStack.push(0);
        minStack.push(1);
        int x1 = minStack.getMin();
        int x2 = minStack.top();
        minStack.pop();
        int x3 = minStack.getMin();
    }
}
